/*
 * @Description:
 * @Author: cgt
 * @Date: 2021-12-23 14:05:56
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-28 18:54:15
 */
function parsePoint(coordinates) {
  return `Point(${coordinates.join(' ')})`;
}
function parseLineString(coordinates) {
  return `LineString(${coordinates.map((item) => item.join(' ')).join(',')})`;
}
function parsePolygon(coordinates) {
  return `Polygon(${coordinates
    .map((temp1) => '(' + temp1.map((item) => item.join(' ')).join(',') + ')')
    .join(',')})`;
}
function parseMultiLineString(coordinates) {
  return `MultiLineString(${coordinates
    .map((temp1) => '(' + temp1.map((item) => item.join(' ')).join(',') + ')')
    .join(',')})`;
}

/**
 * Geojson格式转WKT格式
 * @param {Object} geojson
 */
export function parse(geojson) {
  if (!geojson) {
    console.error('参数不正确', geojson);
    return null;
  }
  var { type, coordinates } = geojson;
  var result = null;
  var fnameMapping = {
    Point: parsePoint,
    LineString: parseLineString,
    Polygon: parsePolygon,
    MultiLineString: parseMultiLineString
  };
  if (fnameMapping[type]) {
    result = fnameMapping[type](coordinates);
  } else {
    console.log('未知类型', type);
  }

  return result;
}
